package xyz.comfyz.web.commons.security.provider.token.impl;

import xyz.comfyz.web.commons.utils.WebContextUtil;

import javax.servlet.http.Cookie;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author :    comfy
 * @date :      2018-05-12 16:04:49
 * @since :     1.8
 * <p>
 *     读取器 从cookie中读取token
 */
public final class CookieTokenReader {

    public String read(final String cookieName) {
        final AtomicReference<String> secret = new AtomicReference<>();
        final Cookie[] cookies = WebContextUtil.request().getCookies();
        if (cookies != null)
            Arrays.stream(cookies)
                    .filter(cookie ->
                            cookieName.equals(cookie.getName()))
                    .findFirst().map(Cookie::getValue).ifPresent(secret::set);
        return secret.get();
    }

}
